//
//  InsuranceDeviceDetailsViewController.m
//  PhotoAnnotate
//
//  Created by Bassem El-Tamimi on 25.01.11.
//  Copyright 2011 ePapyrus GmbH. All rights reserved.
//

#import "InsuranceDeviceDetailsViewController.h"
#import "Insurance.h"

#define FEATURES_TAB 1
#define INSURE_TAB 2
#define STATUS_TAB 3


@implementation InsuranceDeviceDetailsViewController

@synthesize insuranceData;
@synthesize datePicker;
@synthesize aYearAndABit;
@synthesize invalidDateFlag;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = NSLocalizedString(@"INSURANCE_DEVICE_DETAILS_TITLE", @"Device Details");
	self.navigationController.navigationBar.translucent = NO;
	self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
		
	//UIDevice *aDevice = [UIDevice currentDevice];
	
	[deviceName setText:[self.insuranceData deviceString]];

	//[deviceName setText:[NSString stringWithFormat:@"%@, UDID:", [aDevice localizedModel]]];
	//[deviceUDID setText:[aDevice uniqueIdentifier]];
	//[purchaseDateLabel setText:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_LABEL", @"Purchase Date")];
	//[tooOldToInsureText setText:@""];

	//NSLog(@"%@", [aDevice model]);
	
	
	if ([[self.insuranceData.purchaseDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]] compare:[NSDate dateWithTimeIntervalSince1970:[[NSTimeZone localTimeZone] secondsFromGMT]]] != NSOrderedSame) {
		datePicker.date = self.insuranceData.purchaseDate;
	} else {
		NSDate *today = [NSDate date];
		datePicker.date = today;
	}
	

	//datePicker.maximumDate = today;
	
	self.aYearAndABit = -1.0 * (365.0 + 30) * 24.0 * 60.0 * 60.0;
	//datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:aYearAndABit];
    
    NSCalendar *gregorian = [[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar];
	datePicker.calendar = gregorian;
    [gregorian release];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self.navigationController setNavigationBarHidden:NO animated:YES];
	[self.datePicker setMinimumDate:[NSDate dateWithTimeInterval:self.aYearAndABit sinceDate:[NSDate date]]];
	[self.datePicker setMaximumDate:[NSDate date]];
}

- (IBAction)dateChanged:(id)sender
{	
	
	if ([datePicker.date compare:[NSDate date]] == NSOrderedDescending) {
		//[tooOldToInsureText setText:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE", @"Date of Purchse needs to be in the past.")];
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE_ALERT", @"Wrong Date") 
														message:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE", @"Date of Purchse needs to be in the past.") 
													   delegate:self 
											  cancelButtonTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE_ALERT_OK", @"OK") 
											  otherButtonTitles: nil];
		[alert show];	
		[alert release];
		
	} else if (([datePicker.date timeIntervalSinceDate:[NSDate date]] - self.aYearAndABit) < 0.0) {
		//[tooOldToInsureText setText:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_TOO_OLD", @"Device to be insured may not be more than a year old.")];
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE_ALERT", @"Wrong Date") 
														message:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_TOO_OLD", @"Device to be insured may not be more than a year old.") 
													   delegate:self 
											  cancelButtonTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IN_THE_FUTURE_ALERT_OK", @"OK") 
											  otherButtonTitles: nil];
		[alert show];	
		[alert release];
		
	} else {
		//[tooOldToInsureText setText:@""];
		self.insuranceData.purchaseDate = datePicker.date;
		[self.insuranceData updatePropertyListData];
		[self.insuranceData saveChanges];
	}

}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSDate *today = [NSDate date];
	[datePicker setDate:today animated:YES];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
//
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//	
//	if (viewController != self) {
//
//	}
//
//	
//}

- (void)viewWillDisappear:(BOOL)animated
{	
	
	[super viewWillDisappear:animated];
	
	// save the date
	self.insuranceData.purchaseDate = datePicker.date;
	[self.insuranceData updatePropertyListData];
	[self.insuranceData saveChanges];
	
	[self.navigationController setNavigationBarHidden:YES animated:NO];
	// check if datePicker is todays date.
	
	int day1 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:datePicker.date];
	int day2 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:[NSDate date]];
	
	if (day1 == day2) {
		// If so ask user if the device was purchased today			
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IS_TODAY_INQUIRY_ALERT", @"Purchased Today?") 
														message:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IS_TODAY_INQUIRY", @"Did you actually purchase your device today?") 
													   delegate:self.insuranceData 
											  cancelButtonTitle:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IS_TODAY_INQUIRY_NO_BUTTON_TITLE", @"NO") 
											  otherButtonTitles:NSLocalizedString(@"INSURANCE_DEVICE_PURCHASE_DATE_IS_TODAY_INQUIRY_YES_BUTTON_TITLE", @"YES"), nil];
		alert.tag = 0;
		[alert show];	
		[alert release];
		
	}
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
